<?php
require "../includes/db.php"; 
$mysqli = new MossCMS();
if ($_SESSION[$prefix.'_username'] == ""){  header("Location: ../login.php"); exit;}
if ($_SESSION[$prefix."_security"] < 1){header("location:../login.php?action=5"); exit;}




if ($_SERVER['REQUEST_METHOD'] == "POST") { 

$title     	 	= $_POST['title'];
$added    		= $_POST['added'];
$pdate			= $_POST['post_date'];

$active			= $_POST['active'];
$date           = $_POST['post_date'];
$date_prec		= date("Y-m-d H:i:s");
$url			= $_POST['url'];
$id				= $_POST['id'];

$doclink		= $_POST['doclink'];

$delete		= $_POST['delete'];
	
	if($delete == "DELETE"){
		$mysqli->content_remove($id);
		$mysqli->admin_action($z_user, "Deleted doc/link $title ($id)");
		header("Location:doc_list.php"); exit;
	}


$current_upload_file_loc = '';
if(basename($_FILES['location']['name']) != ''){

	include "includes/cleanfile.php";
	include "includes/rmkdir.php"; 
	$upload_folder = "/docs/"; 
	$temp_loc = $global_upload_dir.$upload_folder;
	$http_loc = "uploads".$upload_folder;
	$new_name = cleanfile($title);
	$stamp = date("His");
	
	mkdir($temp_loc);

	$current_upload_file_loc = $_FILES['location']['tmp_name'];
	$current_upload_file_name = basename($_FILES['location']['name']);
		$name_array = explode(".", $current_upload_file_name);
		$file_part1		= $name_array[0];
		$file_part2		= $name_array[1];
		$current_upload_file_name = $file_part1.$stamp.".".$file_part2;
	
	
	if(move_uploaded_file($_FILES['location']['tmp_name'], $temp_loc.$current_upload_file_name)){
		$current_upload_file_loc = $temp_loc.$current_upload_file_name;
	}
}//IF FILE EXISTS


$loc = $current_upload_file_loc;

$type = 2;

if($doclink == 'link'){$loc = $url;$type = 3;}

$thumb = ''; $image = ''; $content = '';
		
		$mysqli->content_edit($id,$type,$title,$image,$thumb,$pdate,$content,$loc);
		
	$mysqli->content_group_remove($id);
	
	$results = $mysqli->groups_list();
   	for($x=0; $x < sizeof($results); $x++) {
      $current_group = $results[$x]['group_id'];
		if($_POST[$current_group] != ''){
			
			$mysqli->content_group_add($id, $current_group);
	
		}	

	}	
		
		
		$mysqli->admin_action($z_user, "Edited document/link $title for $pdate ($id)");

header("Location:doc_list.php");

	
}//END POST

 
$in_id = (int)$_GET{'id'};
$content_info = $mysqli->get_content($in_id);
$content_groups = $mysqli->get_content_groups($in_id);

for($x = 0; $x < sizeof($content_groups); $x++){
	$groups_array[$x] = $content_groups[$x]['group_id'];
}

$date = date("m/d/Y");

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../includes/admin.css"/>
<title><?php echo $global_title;?></title>

<?php echo $jquery_global; ?>
<?php echo $jquery_ui_global; ?>

<script> 
	$(function() {
		$( "#post_date" ).datepicker({ dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true });
	});
	</script> 	


<script language="javascript" type="text/javascript">
    var submitcount = 0;
    function verify()
    {
      // Check for empty fields
      
      if(this.document.form1.doctype.value == "DUMB")
      {
        alert("Please Select a Document Type.");
        this.document.form1.doctype.focus();
        return false;
      }
      
    
	  
	      if(this.document.form1.title.value == "")
      {
        alert("Please Enter a Title.");
        this.document.form1.title.focus();
        return false;
      }
	  
	        
      if(submitcount == 0)
      {
        submitcount++;
        return true;
      }
      else
      {
        alert("This form has already been submitted.  Thanks!");
        return false;
      }
    }
</SCRIPT>


<script type="text/javascript">
function toggle_slide() {
    	var e = document.getElementById('url_select');
       $(e).toggle();
       
       	var e = document.getElementById('upload_select');
       $(e).toggle();
       
       document.getElementById('url').value = '';
       document.getElementById('location').value = '';

    }
</script>

</head>
<body>

<div id="wrap">
	<div id="page-header">
		<?php include('includes/header.php');?>

	</div>
	
	<div id="page-body">
		<div id="tabs">
			<?php $tab="main"; include('includes/header_tabs.php');?>
		</div>

		<div id="acp">
		<div class="panel">
			<span class="corners-top"><span></span></span>
				<div id="content">
				<div id="menu">
						
					<?php include('includes/menu_top.php');?>	
					<?php include('includes/menu_main.php');?>
					</div>
	
					<div id="main">
<a name="maincontent"></a>


	<h1>Add Document/Link</h1>


	<form id="form1" name="form1" enctype="multipart/form-data" method="post" action=""  onSubmit="return verify();">
      <input type="hidden" name="active" value="FALSE">
      <input type="hidden" name="id" value="<?php echo $in_id; ?>"> 
    <fieldset><legend>Edit Document/Link</legend>   
        
        <dl>
            <dt>Document or Link:</dt>
            <dd>
            
            Document<input type="radio" name="doclink" id="doclink" value="doc" <?php if($content_info['content_type'] == 2){ echo " checked ";} ?>   onchange="toggle_slide();"> 
    			Link<input type="radio" name="doclink" id="doclink" value="link" <?php if($content_info['content_type'] == 3){ echo " checked ";} ?>   onchange="toggle_slide();">
			
	           </dd>
          </dl>
        
        <dl>
            <dt>Title:</dt>
            <dd><input type="text" name="title" id="title" size='60' autofocus required value="<?php echo $content_info['content_title'];?>" ></dd>
          </dl>
         <dl title="The date the content will go live on the site">
            <dt>Post Date:</dt>
            <dd><input type="text" name="post_date" id="post_date" value="<?php echo $content_info['content_date'];?>"/></dd>
          </dl>

       <dl id="upload_select">
            <dt>Upload:</dt>
            <dd><input type="file" name="location" id="location"  /></dd>
          </dl>
          
        <dl id="url_select" style="display:none;">
            	<dt>Link URL:</dt>
    			<dd><input name="url" id="url" type="text" size="60" placeholder="http://www...." value="<?php echo $content_info['content_loc'];?>"></dd>
    		</dl>
    		
    	<!--  	
          <dl>
            <dt>Active:</dt>
            <dd><input name="active" type="checkbox" id="active" value="TRUE" checked="checked" /></dd>
          </dl>
        -->
        
        <dl>
             <dt>DELETE DOC/LINK:</dt>
             <dd><input type="checkbox" name="delete" id="delete" value="DELETE"  /></dd>
        </dl>
        
       </fieldset>
       
  <fieldset><legend>Access</legend>
         <p>Assign which groups will have access to this content</p>  
         <table border=0 width="100%">
			<tr>
         <?php $y=0;
         $results = $mysqli->groups_list();
      			for($x=0; $x < sizeof($results); $x++) {
      			$y++;
      			if($y == 5){echo "</tr><tr>"; $y= 0;}
      			if( in_array($results[$x]['group_id'], $groups_array)){$checked = " checked ";}else{$checked = '';}
         	?>
         	<td><input type="checkbox" id="<?php echo $results[$x]['group_id'];?>" name="<?php echo $results[$x]['group_id'];?>" value="<?php echo $results[$x]['group_id'];?>"<?php echo $checked;?> > <?php echo $results[$x]['group_name'];?></td>
         	<?php } ?>
           </tr></table>	
        </fieldset>       
        
        
         <br>

<p class="submit-buttons"><input type="submit" class="button1" name="submit" id="submit" value="Update Content" /></p>

        </fieldset>
        <p>**Post Date = The date the content will go live on the website</p>

      <input type="hidden" name="subtype1" id="subtype1" value="1">
    </form>


<?php if($content_info['content_type'] == 3){
?>

<script type="text/javascript">

    	var e = document.getElementById('url_select');
       $(e).toggle();
       
       	var e = document.getElementById('upload_select');
       $(e).toggle();
       
</script>
<?php
} ?>

    
		</div><!-- main -->
	</div><!-- content -->
<span class="corners-bottom"><span></span></span>
</div><!-- panel -->
</div><!-- acp -->

	
	<div id="page-footer">
	<?php include('includes/footer.php');?>
				</div>
</div><!-- page-body -->
</div><!-- wrap -->


</body> 
</html>